import $C from './config.js';
import $U from './util.js';
import $store from '@/store/index.js';

export default {
    // 全局配置
    common: {
        baseUrl: $C.baseUrl,
        header: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
        method: 'GET',
        dataType: 'json',
        token: true
    },

    // 请求 返回promise
    request(options = {}) {
        // 组织参数
        options.url = this.common.baseUrl + options.url;
        options.header = { ...this.common.header, ...options.header };
        options.data = { ...this.common.data, ...options.data };
        options.method = options.method || this.common.method;
        options.dataType = options.dataType || this.common.dataType;
        options.token = options.token === false ? false : this.common.token;
        // 请求之前验证...
        // token验证
        if (options.token) {
            let token = $U.getStorage('token');
            // 二次验证
            if (!token) {
                uni.showToast({ title: '请先登录', icon: 'none' });
                // token不存在时跳转
                return uni.reLaunch({ url: '/pages/login/login' });
            }
            // 往header头中添加token
            options.header['X-Access-Token'] = token;
        }

        // 请求
        return new Promise((resolve, reject) => {
            // 请求中...
            uni.request({
                ...options,
                success: (result) => {
                    // 返回原始数据
                    if (options.native) {
                        return resolve(result);
                    }
                    // 服务端失败
                    if (result.statusCode !== 200) {
                        if (options.toast !== false) {
                            uni.showToast({ title: result.data || '服务端失败', icon: 'none' });
                        }
                        // token不合法，直接退出登录
                        if (result.data === 'Token 令牌不合法!') {
                            $store.dispatch('logout');
                        }
                        return reject(result.data);
                    }
                    // 其他验证...
                    // 成功
                    let data = result.data;
                    resolve(data);
                },
                fail: (error) => {
                    uni.showToast({ title: error.errMsg || '请求失败', icon: 'none' });
                    reject(error);
                }
            });
        });
    },

    // get请求
    get(url, data = {}, options = {}) {
        return this.request({ ...options, url, data, method: 'GET' });
    },

    // post请求
    post(url, data = {}, options = {}) {
		console.log(url, data,'0000000000')
        return this.request({ ...options, url, data, method: 'POST' });
    },

    // delete请求
    del(url, data = {}, options = {}) {
        return this.request({ ...options, url, data, method: 'DELETE' });
    },

    // 上传文件
    upload(url, data, onProgress = false) {
        return new Promise((resolve, reject) => {
            // 上传
            let token = $U.getStorage('token');
            if (!token) {
                uni.showToast({ title: '请先登录', icon: 'none' });
                // token不存在时跳转
                return uni.reLaunch({ url: '/pages/login/login' });
            }

            const uploadTask = uni.uploadFile({
                url: this.common.baseUrl + url,
                filePath: data.filePath,
                name: data.name || "files",
                header: { token },
                success: (res) => {
                    if (res.statusCode !== 200) {
                        resolve(false);
                        return uni.showToast({ title: '上传失败', icon: 'none' });
                    }
                    let message = JSON.parse(res.data);
                    resolve(message.data);
                },
                fail: (err) => {
                    console.log(err);
                    reject(err);
                }
            });

            uploadTask.onProgressUpdate((res) => {
                if (typeof onProgress === 'function') {
                    onProgress(res.progress);
                }
            });
        });
    },
    getFullImageUrl(url){
        return $C.baseUrl+'/' + url;
    }
}
